package org.mcjug.locationmailer.activity;

import org.mcjug.locationmailer.LocationMailerApplication;
import org.mcjug.locationmailer.R;
import org.mcjug.locationmailer.receiver.ExceptionBroadcastReceiver;
import org.mcjug.locationmailer.util.NetworkConnectivity;
import org.mcjug.locationmailer.util.ParcelableException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class BaseActivity extends Activity {
    private ExceptionReceiver exceptionReceiver;
    private LocationMailerApplication locationMailerApp;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        exceptionReceiver = new ExceptionReceiver();
        locationMailerApp = (LocationMailerApplication) getApplication();
    }

    protected void onResume() {
        super.onResume();

        // Check for notification intent
        Intent intent = getIntent();
        if (intent.getAction() != null
                && intent.getAction().equals(getString(R.string.intentDisplayExceptionActionName))) {
            displayExceptionDialog(intent);
        }

        // Register to receive exception messages
        IntentFilter filter = new IntentFilter(getString(R.string.intentDisplayExceptionActionName));
        registerReceiver(exceptionReceiver, filter);

        locationMailerApp.setActivityRunning(true);
    }

    protected void onPause() {
        // Unregister the exception receiver
        unregisterReceiver(exceptionReceiver);

        locationMailerApp.setActivityRunning(false);

        super.onPause();
    }

    // Listens for exception messages
    private class ExceptionReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            displayExceptionDialog(intent);
        }
    }

    private void displayExceptionDialog(Intent intent) {
        ParcelableException parcelableException = intent.getParcelableExtra(ExceptionBroadcastReceiver.EXCEPTION);
        String stackTrace = parcelableException.getMessage();

        ExceptionDialog dialog = new ExceptionDialog(BaseActivity.this, stackTrace);
        dialog.show();
    }
    
    protected boolean checkNetworkConnection() {
    	boolean networkAvailable = NetworkConnectivity.isAvailable(this);
    	if (!networkAvailable) {
    		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    		alertDialog.setTitle(getString(R.string.networkConnectionNotAvailableTitle));
    		alertDialog.setMessage(getString(R.string.networkConnectionNotAvailableMsg));
    		alertDialog.setButton(getString(R.string.buttonOk), new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int which) {
    				alertDialog.cancel();
    			}
    		});
    		alertDialog.show();		
    	}
    	return networkAvailable;
    }
}
